package org.spring.sample.common.restful.controller;
/**
 * RESTFUL请求controller
 */
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.sample.restful.dto.Person;
import org.spring.sample.restful.dto.Response;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);

	@GetMapping(value = "/person")
	public Response getPerson() {
		List<Person> list = new ArrayList<>();
		for (int i = 0; i < 3; i++) {
			Person person = new Person();
			person.setName("张三" + i);
			person.setAddress("深圳市" + i);
			person.setTel("15800000000" + i);
			list.add(person);
		}
		return new Response().success(list);
	}

	@GetMapping(value = "/person/{tel}")
	public Response getPerson(@PathVariable("tel") String tel) {
		logger.info("请求参数{}", tel);
		return new Response().success();
	}

	@PutMapping(value = "/person/{tel}", consumes = "application/json")
	public Response putPerson(@PathVariable("tel") String tel, @RequestBody Person person) {
		logger.info("请求参数{},更新参数{}", tel, person.toString());
		return new Response().success();
	}

	@PostMapping(value = "/person", consumes = "application/x-www-form-urlencoded")
	public Response postPerson(Person person, HttpServletRequest request) {
		logger.info("post请求");
		return null;
	}
	
	@DeleteMapping(value="/person/{tel}",consumes = "application/x-www-form-urlencoded")
	public Response deletePerson(@PathVariable(value="tel") String tel) {
		logger.info("delete请求{}",tel);
		return new Response().success();
	}
}
